package com.dongdong.bookstore.module.ums.controller;


import com.dongdong.bookstore.common.exception.Asserts;
import com.dongdong.bookstore.module.ums.model.Menu;
import com.dongdong.bookstore.module.ums.model.Resource;
import com.dongdong.bookstore.module.ums.model.Role;
import com.dongdong.bookstore.module.ums.service.RoleService;
import com.dongdong.bookstore.module.ums.viewmodel.RoleParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author chiangkai
 * @since 2021-10-20
 */
@RestController
@Api(tags = "后台角色管理")
@RequestMapping("/ums/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation("添加管理员角色")
    @PostMapping("/create")
    public String createRole(@RequestBody RoleParam roleParam) {
        roleService.create(roleParam);
        return "添加角色成功";
    }

    @ApiOperation("更新角色信息")
    @PostMapping("/update/{id}")
    public String updateRole(@PathVariable Long id, @RequestBody RoleParam roleParam) {
        roleService.update(id, roleParam);
        return "更新成功";
    }

    @ApiOperation("批量删除角色")
    @PostMapping("/delete")
    public String delete(@RequestParam("ids") List<Long> ids) {
        boolean success = roleService.delete(ids);
        Asserts.isFalse(success, "删除角色失败");
        return "删除角色成功";
    }

    @ApiOperation("修改角色状态")
    @PostMapping(value = "/updateStatus/{id}")
    public String updateStatus(@PathVariable Long id, @RequestParam(value = "status") Integer status) {
        boolean success = roleService.updateStateById(id, status);
        Asserts.isFalse(success, "更新状态失败");
        return "修改状态成功";
    }

    @ApiOperation("获取角色相关菜单")
    @GetMapping("/menuList/{roleId}")
    public List<Menu> menuList(@PathVariable Long roleId) {
        return roleService.menuListByRoleId(roleId);
    }
    @ApiOperation("获取所有角色")
    @GetMapping("/listAll")
    public List<Role> listAll(){
        return roleService.list();
    }

    @ApiOperation("获取角色相关资源")
    @GetMapping("/resourceList/{roleId}")
    public List<Resource> resourceList(@PathVariable Long roleId) {
        return roleService.resourceListByRoleId(roleId);
    }

    @ApiOperation("给角色分配菜单")
    @PostMapping("/allocateMenu")
    public String allocateMenu(@RequestParam Long roleId, @RequestParam List<Long> menuIds) {
        roleService.allocateMenu(roleId, menuIds);
        return "分配菜单成功";
    }

    @ApiOperation("给角色分配资源")
    @PostMapping("/allocateResource")
    public String allocateResource(@RequestParam Long roleId, @RequestParam List<Long> resourceIds) {
        roleService.allocateResource(roleId, resourceIds);
        return "分配资源成功";
    }

}
